//
//  YWNoQrcodeMaintainRecordTableViewCell.m
//  maintain
//
//  Created by 烟雨云渊 on 16/12/9.
//  Copyright © 2016年 ewin. All rights reserved.
//

#import "YWNoQrcodeMaintainRecordTableViewCell.h"
#import "GropPictureView.h"
#import "YWGropButtonView.h"
#import "Masonry.h"
#import "YWMacro.h"
#import "UIColor+CustomColors.h"
#import "YWCommonTool.h"
#import "UIImageView+WebCache.h"

#import "YWInspectionEquipmentRecord.h"
#import "YWEquipmentTroubleRecord.h"
#import "YWHandler.h"
#import "YWEquipmentMaintenanceRecord.h"
#import "YWDictMaintenanceType.h"
#import "YWKeepWatchMissionManager.h"
#import "YWMissionManager.h"
#import "YYLabel.h"

@interface YWNoQrcodeMaintainRecordTableViewCell()
/** 头像 */
@property (nonatomic, strong) UIImageView *headPortraitView;
/** 名字 */
@property (nonatomic, strong) YYLabel *nameLabel;
/** 时间 */
@property (nonatomic, strong) YYLabel *timeLabel;
/** 状态 */
@property (nonatomic, strong) YYLabel *statusLabel;
/** 编号 */
@property (nonatomic, strong) YYLabel *numberLabel;
/** 备注或者说明 */
@property (nonatomic, strong) YYLabel *noteLabel;
/** 图片容器 */
@property (nonatomic, strong) GropPictureView *pictureGroupContentView;
/** 图片高的约束 */
@property (nonatomic, strong) MASConstraint *picHeightConstraint;
@end

@implementation YWNoQrcodeMaintainRecordTableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.layer.shouldRasterize = YES;
        self.layer.rasterizationScale = [UIScreen mainScreen].scale;
        [self setUpSubviews];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}

- (void)setInspectionRecord:(YWInspectionEquipmentRecord *)inspectionRecord{
    _inspectionRecord = inspectionRecord;
    NSString* downloadUrl = [YWCommonTool pictureDownloadUrl:_inspectionRecord.executor.headIcon];
    //    头像
    [self.headPortraitView sd_setImageWithURL:[NSURL URLWithString:downloadUrl] placeholderImage:[UIImage imageNamed:@"profile_headimage"]];
    //时间
    NSString *timeStr = [YWCommonTool showTimeWithNormalFormatter:_inspectionRecord.executeTime/1000];
//    [_timeLabel labelText:@"巡查时间: " withAttributedText:timeStr andAttributedColor:[UIColor customYellowColor]];
    self.timeLabel.text = timeStr;
    //    名字
    NSString * inspector = _inspectionRecord.executor.userName ?:@"无";
//    [_nameLabel labelText:@"巡查人: " withAttributedText:inspector andAttributedColor:[UIColor customYellowColor]];
    self.nameLabel.text = inspector;
    //编号
    _numberLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"编号: " firstColor:[UIColor customTextGrayColor] secondText:@"暂无" secondColor:[UIColor customYellowColor] font:kContentFont];
    //备注
    NSString*noteText = _inspectionRecord.note.length>0?_inspectionRecord.note:@"无";
    _noteLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"备注: " firstColor:[UIColor customTextGrayColor] secondText:noteText secondColor:[UIColor blackColor] font:kContentFont];
    //    图片集
    NSMutableArray * recordPictures = [NSMutableArray array];
    if (_inspectionRecord.pictures.count) {
        [recordPictures addObjectsFromArray:_inspectionRecord.pictures];
    }else{
        [recordPictures addObjectsFromArray:[[YWKeepWatchMissionManager shareManager]recordPictureWithRecordId:_inspectionRecord.equipmentRecordId recordType:YWPictureRecordTypeInspectionMission]];
    }
    [_pictureGroupContentView initWithGroupItems:recordPictures withConstraint:nil];
    
    self.noteLabel.preferredMaxLayoutWidth = self.noteLabel.frame.size.width;
}

- (void)setTroubleRecord:(YWEquipmentTroubleRecord *)troubleRecord{
    _troubleRecord = troubleRecord;
    NSString* downloadUrl = [YWCommonTool pictureDownloadUrl:_troubleRecord.handler.headIcon];
    //    头像
    [self.headPortraitView sd_setImageWithURL:[NSURL URLWithString:downloadUrl] placeholderImage:[UIImage imageNamed:@"profile_headimage"]];
    //时间
    NSString *timeStr = [YWCommonTool showTimeWithNormalFormatter:_troubleRecord.completeTime/1000];
    self.timeLabel.text = timeStr;
    self.nameLabel.text = _troubleRecord.handlerName?:@"无";
    //编号
    NSString *recordSequence = _troubleRecord.recordSequence.length>0?_troubleRecord.recordSequence:@"暂无";
    _numberLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"编号: " firstColor:[UIColor customTextGrayColor] secondText:recordSequence secondColor:[UIColor customYellowColor] font:kContentFont];
    //备注
    NSString* noteText = _troubleRecord.note.length>0?_troubleRecord.note:@"无";
    _noteLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"备注: " firstColor:[UIColor customTextGrayColor] secondText:noteText secondColor:[UIColor blackColor] font:kContentFont];
    //记录的图片
    NSMutableArray * recordPictures = [NSMutableArray array];
    if (_troubleRecord.recordPictures.count) {
        [recordPictures addObjectsFromArray:_troubleRecord.recordPictures];
    }else{
        [recordPictures addObjectsFromArray:[[YWMissionManager shareManager] picturesWithRecordId:_troubleRecord.recordId maintenanceType:_troubleRecord.maintenanceTypeId]];
    }
    //    图片集
    [_pictureGroupContentView initWithGroupItems:recordPictures withConstraint:nil];
    
    self.noteLabel.preferredMaxLayoutWidth = self.noteLabel.frame.size.width;
}

- (void)setMaintenceRecord:(YWEquipmentMaintenanceRecord *)maintenceRecord{
    _maintenceRecord = maintenceRecord;
    NSString* downloadUrl = [YWCommonTool pictureDownloadUrl:_maintenceRecord.handler.headIcon];
    //    头像
    [self.headPortraitView sd_setImageWithURL:[NSURL URLWithString:downloadUrl] placeholderImage:[UIImage imageNamed:@"profile_headimage"]];
    if (maintenceRecord.maintenanceTypeId == YWMaintenanceTypeDetection) {
        //    名字
        _nameLabel.text = _maintenceRecord.handlerName?:@"无";
        //   时间
        NSString *timeStr = [YWCommonTool showTimeWithNormalFormatter:_maintenceRecord.completeTime/1000];
        self.timeLabel.text = timeStr;
    }else{
        //    名字
        self.nameLabel.text = _maintenceRecord.handlerName?:@"无";
        //   时间
        NSString *timeStr = [YWCommonTool showTimeWithNormalFormatter:_maintenceRecord.completeTime/1000];
        self.timeLabel.text = timeStr;
    }
    //   编号
    NSString *recordSequence = _maintenceRecord.recordSequence.length>0?_maintenceRecord.recordSequence:@"无";
    _numberLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"编号: " firstColor:[UIColor customTextGrayColor] secondText:recordSequence secondColor:[UIColor customYellowColor] font:kContentFont];
    //  备注
    NSString* noteText = _maintenceRecord.note.length>0?_maintenceRecord.note:@"无";
    _noteLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"备注: " firstColor:[UIColor customTextGrayColor] secondText:noteText secondColor:[UIColor blackColor] font:kContentFont];
    //记录的图片
    NSMutableArray * recordPictures = [NSMutableArray array];
    if (_maintenceRecord.recordPictures.count) {
        [recordPictures addObjectsFromArray:_maintenceRecord.recordPictures];
    }else{
        [recordPictures addObjectsFromArray:[[YWMissionManager shareManager] picturesWithRecordId:_maintenceRecord.recordId maintenanceType:_maintenceRecord.maintenanceTypeId]];
    }
    //    图片集
    [_pictureGroupContentView initWithGroupItems:recordPictures withConstraint:nil];
    
    self.noteLabel.preferredMaxLayoutWidth = self.noteLabel.frame.size.width;
}

- (void)setUpSubviews{
    /**
     *  头像 名字    状态
     *  头像 时间
     */
    [self.headPortraitView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.top.mas_equalTo(self.contentView).offset(8);
        make.width.and.height.mas_equalTo(@46);
    }];
    
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(_headPortraitView.mas_centerY).offset(-2);
        make.left.mas_equalTo(_headPortraitView.mas_right).offset(6);
    }];
    
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_nameLabel);
        make.top.mas_equalTo(_nameLabel.mas_bottom).offset(4);
    }];
    
    [self.statusLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.baseline.mas_equalTo(_nameLabel.mas_baseline);
        make.right.mas_equalTo(self.contentView).offset(-8);
    }];
    
    [self.numberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_headPortraitView.mas_bottom).offset(6);
        make.left.mas_equalTo(self.contentView).offset(8);
        make.right.mas_equalTo(self.contentView).offset(-8);
    }];
    
    [self.noteLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_numberLabel.mas_bottom).offset(6);
    }];
    
    [self.pictureGroupContentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_noteLabel.mas_bottom).offset(6).priorityHigh();
        make.left.and.right.mas_equalTo(@[_numberLabel,_noteLabel]);
        _picHeightConstraint = make.height.mas_equalTo(@90).priorityHigh();
        make.bottom.mas_equalTo(self.contentView).priorityHigh();
    }];
}

//- (void)layoutSubviews{
//    [super layoutSubviews];
//   
//}


- (UIImageView *)headPortraitView{
    if (!_headPortraitView) {
        UIImageView *imageView = [UIImageView new];
        imageView.backgroundColor = [UIColor customTextGrayColor];
        imageView.clipsToBounds = YES;
        imageView.layer.cornerRadius = 46/2;
        _headPortraitView = imageView;
        [self.contentView addSubview:_headPortraitView];
    }
    return _headPortraitView;
}

- (YYLabel *)nameLabel{
    if (!_nameLabel) {
        YYLabel *label = [[YYLabel alloc] init];
        label.font = kContentFont;
        label.textColor = [UIColor customYellowColor];
        _nameLabel = label;
        [self.contentView addSubview:_nameLabel];
    }
    return _nameLabel;
}

- (YYLabel *)timeLabel{
    if (!_timeLabel) {
        YYLabel *label = [[YYLabel alloc] init];
        label.font = kContentFont;
        label.textColor = [UIColor customTextGrayColor];
        _timeLabel = label;
        [self.contentView addSubview:_timeLabel];
    }
    return _timeLabel;
}

- (YYLabel *)statusLabel{
    if (!_statusLabel) {
        YYLabel *label = [[YYLabel alloc] init];
        label.font = kContentFont;
        label.textColor = [UIColor customTextGrayColor];
        _statusLabel = label;
        [self.contentView addSubview:_statusLabel];
    }
    return _statusLabel;
}

- (YYLabel *)numberLabel{
    if (!_numberLabel) {
        YYLabel *label = [[YYLabel alloc] init];
        _numberLabel = label;
        [self.contentView addSubview:_numberLabel];
    }
    return _numberLabel;
}

- (YYLabel *)noteLabel{
    if (!_noteLabel) {
        YYLabel *label = [[YYLabel alloc] init];
        label.numberOfLines = 0;
        _noteLabel = label;
        [self.contentView addSubview:_noteLabel];
    }
    return _noteLabel;
}

-(GropPictureView *)pictureGroupContentView{
    if (!_pictureGroupContentView) {
        GropPictureView *gropPictureView = [GropPictureView new];
        _pictureGroupContentView = gropPictureView;
        [self.contentView addSubview:_pictureGroupContentView];
    }
    return _pictureGroupContentView;
}
@end
